package com.baseframe.topic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baseframe.auth.utils.AuthUtil;
import com.baseframe.mybatis.utils.BulkUtil;
import com.baseframe.score.feign.SnsScoreFeign;
import com.baseframe.tool.result.R;
import com.baseframe.topic.entity.SnsTestTopicItemOption;
import com.baseframe.topic.entity.SnsTestTopicUser;
import com.baseframe.topic.entity.SnsTestTopicUserResult;
import com.baseframe.topic.entity.request.SnsTestTopicRequest;
import com.baseframe.topic.entity.request.SnsTestTopicUserRequest;
import com.baseframe.topic.mapper.SnsTestTopicUserMapper;
import com.baseframe.topic.service.SnsTestTopicUserResultService;
import com.baseframe.topic.service.SnsTestTopicUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**

* @description 针对表【sns_test_topic_user(用户参与能测记录表)】的数据库操作Service实现
* @createDate 2023-07-08 20:58:20
*/
@Service
@AllArgsConstructor
@Slf4j
public class SnsTestTopicUserServiceImpl extends ServiceImpl<SnsTestTopicUserMapper, SnsTestTopicUser>
    implements SnsTestTopicUserService {


    private BulkUtil bulkUtil;

    private SnsTestTopicUserResultService snsTestTopicUserResultService;

    private SnsScoreFeign snsScoreFeign;

    @Override
    public boolean hasJoin(Long topicId) {
        SnsTestTopicUser one = getOne(new LambdaQueryWrapper<SnsTestTopicUser>()
                .eq(SnsTestTopicUser::getTopicId, topicId)
                .eq(SnsTestTopicUser::getUserId, AuthUtil.getUserId()).last("limit 1"));
        return one!=null;
    }

    @Override
    public boolean submitUserSelectOptions(SnsTestTopicRequest request) {
        boolean insertBulk = bulkUtil.insertBulk(() -> {

            List<SnsTestTopicUser> snsTestTopicUserList = new ArrayList<>();
            //防止 submitUserSelectOptions 在其他地方调用，这里再做一遍是否重复提交的检查
            SnsTestTopicUser one = getOne(new LambdaQueryWrapper<SnsTestTopicUser>()
                    .eq(SnsTestTopicUser::getUserId, AuthUtil.getUserId())
                    .eq(SnsTestTopicUser::getTopicId, request.getId())
                    .last("limit 1")
            );
            for (SnsTestTopicUserRequest userSelectOption : request.getUserSelectOptions()) {

                if (one != null) {
                    break;
                }
                SnsTestTopicUser snsTestTopicUser = new SnsTestTopicUser();
                BeanUtils.copyProperties(userSelectOption, snsTestTopicUser);
                snsTestTopicUserList.add(snsTestTopicUser);
            }

            return snsTestTopicUserList;
        }, SnsTestTopicUserMapper.class);

        if(insertBulk){
            SnsTestTopicUserResult userResult = snsTestTopicUserResultService.getOne(new LambdaQueryWrapper<SnsTestTopicUserResult>().eq(SnsTestTopicUserResult::getUserId, AuthUtil.getUserId()).eq(SnsTestTopicUserResult::getTopicId, request.getId()));

            if(userResult!=null){
                return insertBulk;
            }
            //将成绩写到成绩表上
            SnsTestTopicUserResult snsTestTopicUserResult = new SnsTestTopicUserResult();
            snsTestTopicUserResult.setUserId(AuthUtil.getUserId());
            snsTestTopicUserResult.setTopicId(request.getId());
            Integer result = getBaseMapper().getResult(request.getId());
            snsTestTopicUserResult.setResult(result==null?0:result);
            snsTestTopicUserResultService.save(snsTestTopicUserResult);

            if(snsTestTopicUserResult.getResult() == 0){
                return insertBulk;
            }
            // 将积分写到积分账号上
            R<Object> objectR = snsScoreFeign.increaseScore(snsTestTopicUserResult.getResult(), AuthUtil.getUserId());
            if(objectR.getCode() == 200){
                snsTestTopicUserResult.setToAccount(1);
                //更新积分到账状态
                snsTestTopicUserResultService.updateById(snsTestTopicUserResult);
            }else {
                log.warn("用户:{}的积分:{}未到账成功",AuthUtil.getUsername(), snsTestTopicUserResult.getResult());
            }


        }

        //批量提交
        return insertBulk;
    }

    @Override
    public Integer getTopicResult(SnsTestTopicUserRequest request) {


        return snsTestTopicUserResultService.getResultByTopicIdAndUserId(request.getTopicId(),AuthUtil.getUserId());
    }
}




